#include "createdoc.h"

CreateDoc::CreateDoc()
{
}

CreateDoc::~CreateDoc()
{
}

void CreateDoc::create_doc( QMap<QString, QString> m,
                            QString table,
                            QSqlRecord record )
{
    if ( true == record.isNull("ID") )
    {
    return;
    }

    if ( true == table.isEmpty() )
    {
    return;
    }

QSqlDatabase db = QSqlDatabase::database("collection", false);
    if ( true != db.isOpen() )
    {
    return;
    }

QPrinter printer;
QSizeF page = printer.pageRect().size();

doc.clear();

QString obv_icon = QLatin1String("");
QString rev_icon = QLatin1String("");
QString edge_icon = QLatin1String("");

    if ( m.value("images") == QLatin1String("Y") )
    {
    obv_icon = add_image( table,
                          record.value("ID").toLongLong(),
                          QLatin1String("obverse") );
    rev_icon = add_image( table,
                          record.value("ID").toLongLong(),
                          QLatin1String("reverse") );
    edge_icon = add_image( table,
                           record.value("ID").toLongLong(),
                           QLatin1String("edge") );
    }

Schema * schema = new Schema;
    if ( NULL == schema )
    {
    return;
    }
QMap<int, QString> column_names = schema->column_names();
QMap<int, QString> headers = schema->headers();
QList<int> columns_for_title = schema->columns_for_title();
QList<int> research_columns = schema->research_columns();
QList<int> management_columns = schema->management_columns();

QStringList title;
QMap<int, QVariant> values;

    for ( int x = 1; x < schema->columns_count(); ++x )
    {
        if ( true != record.value( column_names.value(x) ).isNull() )
        {
            if ( true == columns_for_title.contains(x) )
            {
            title << record.value( column_names.value(x) ).toString();
            }
            else
            {
            values[x] = record.value( column_names.value(x) );
            }
        }
    }

QTextCursor cursor( &doc );
cursor.movePosition(QTextCursor::Start);

QTextFrame * topFrame = cursor.currentFrame();
QTextFrameFormat topFrameFormat = topFrame->frameFormat();
topFrameFormat.setPadding(8);
topFrame->setFrameFormat(topFrameFormat);

QTextCharFormat textFormat;
QTextCharFormat boldFormat;
boldFormat.setFontWeight(QFont::Bold);

QTextFrameFormat titleFrameFormat;
titleFrameFormat.setBorder(0);
titleFrameFormat.setPadding(8);
cursor.insertFrame(titleFrameFormat);

QStringListIterator title_i(title);
    while ( true == title_i.hasNext() )
    {
    cursor.insertText( title_i.next(), boldFormat );
    cursor.insertBlock();
    }
cursor.insertBlock();

    if ( m.value("images") == QLatin1String("Y") )
    {
    QTextFrameFormat frameFormat;
    frameFormat.setBorder(0);
    frameFormat.setPadding(8);
    cursor.insertFrame(frameFormat);

        if ( true != obv_icon.isEmpty() &&
             m.value("obv") == QLatin1String("Y") )
        {
        cursor.insertImage(obv_icon);
        cursor.insertText( QLatin1String("    ") );
        }

        if ( true != rev_icon.isEmpty() &&
             m.value("rev") == QLatin1String("Y") )
        {
        cursor.insertImage(rev_icon);
        }

    cursor.insertBlock();
    }

    if ( m.value("images") == QLatin1String("Y") )
    {
    cursor.setPosition( topFrame->lastPosition() );
    QTextFrameFormat frameFormat;
    frameFormat.setBorder(0);
    frameFormat.setPadding(8);
    cursor.insertFrame(frameFormat);

        if ( true != edge_icon.isEmpty() &&
             m.value("edge") == QLatin1String("Y") )
        {
        cursor.insertImage(edge_icon);
        }

    cursor.insertBlock();
    }

cursor.setPosition( topFrame->lastPosition() );

QVector<QTextLength> cw;
cw << QTextLength( QTextLength::PercentageLength, 20 );
cw << QTextLength( QTextLength::PercentageLength, 80 );

QTextTableFormat valuesTableFormat;
valuesTableFormat.setAlignment( Qt::AlignLeft );
valuesTableFormat.setColumnWidthConstraints(cw);
valuesTableFormat.setWidth( QTextLength( QTextLength::PercentageLength, 100 ) );
valuesTableFormat.setBorder(0);
QTextTable * valuesTable = cursor.insertTable( 1, 2, valuesTableFormat );

QMapIterator<int, QVariant> values_i(values);

int count_rows = -1;
    while ( true == values_i.hasNext() )
    {
    values_i.next();
    int k = values_i.key();
        if ( research_columns.contains(k) &&
             m.value("research") != "Y" )
        {
        continue;
        }

        if ( management_columns.contains(k) &&
             m.value("management") != "Y" )
        {
        continue;
        }

    count_rows += 1;
    valuesTable->insertRows(count_rows, 1);
    cursor = valuesTable->cellAt( count_rows, 0 ).firstCursorPosition();
    cursor.insertText( headers.value( values_i.key() ) , boldFormat );
    cursor = valuesTable->cellAt( count_rows, 1 ).firstCursorPosition();
    cursor.insertText( values_i.value().toString(), textFormat );
    }

cursor.setPosition( topFrame->lastPosition() );

    if ( m.value("desc") == QLatin1String("Y") )
    {
    QString desc = add_desc( table, record.value("ID").toLongLong() );
        if ( true != desc.isEmpty() )
        {
        QTextBlockFormat f_break;
            if ( m.value("desc_html") == QLatin1String("Y") )
            {
            f_break.setPageBreakPolicy( QTextFormat::PageBreak_AlwaysBefore );
            }
            else
            {
            f_break.setPageBreakPolicy( QTextFormat::PageBreak_Auto );
            }
        cursor.insertBlock(f_break);
            if ( m.value("desc_html") == QLatin1String("Y") )
            {
            cursor.insertHtml(desc);
            }
            else
            {
            cursor.insertText(desc);
            }
        cursor.setPosition( topFrame->lastPosition() );
        }
    }


    if ( m.value("auctions") == QLatin1String("Y") )
    {
    QList< QMap<QString, QVariant> > l;
    l = add_auctions( table, record.value("ID").toLongLong() );

    QListIterator< QMap<QString, QVariant> > l_i(l);

        if ( l.size() >= 1 )
        {
        QTextBlockFormat f_break;
        f_break.setPageBreakPolicy( QTextFormat::PageBreak_AlwaysBefore );
        cursor.insertBlock(f_break);
        cursor.insertText( QObject::tr("Auctions") , boldFormat );
        QTextBlockFormat f;
        cursor.insertBlock(f);
        cursor.setPosition( topFrame->lastPosition() );
        }
    
        while ( true == l_i.hasNext() )
        {
        QTextBlockFormat f;
        cursor.insertBlock(f);
        cursor.insertBlock(f);

        QMap<QString, QVariant> mr = l_i.next();

        QVector<QTextLength> twc1;
        twc1 << QTextLength( QTextLength::PercentageLength, 70 );
        twc1 << QTextLength( QTextLength::PercentageLength, 30 );
        QTextTableFormat tf1;
        tf1.setAlignment( Qt::AlignLeft );
        tf1.setColumnWidthConstraints(twc1);
        tf1.setWidth( QTextLength( QTextLength::PercentageLength, 100 ) );
        tf1.setBorder(0);
        QTextTable * t1 = cursor.insertTable( 1, 2, tf1 );
        cursor = t1->cellAt( 0, 0 ).firstCursorPosition();
        cursor.insertText( mr.value("auct_name").toString() , textFormat );
        cursor = t1->cellAt( 0, 1 ).firstCursorPosition();
        QString date_str = QLatin1String("");
        QDate date = QDate::fromString( QString::number( mr.value("auct_date").toInt() ),
                                        "yyyyMMdd" );
             if ( true == date.isValid() )
             {
             date_str = date.toString("d MMMM yyyy");
             }
        cursor.insertText( date_str, textFormat );
        cursor.setPosition( topFrame->lastPosition() );

        QVector<QTextLength> twc3;
        twc3 << QTextLength( QTextLength::PercentageLength, 70 );
        twc3 << QTextLength( QTextLength::PercentageLength, 30 );
        QTextTableFormat tf3;
        tf3.setAlignment( Qt::AlignLeft );
        tf3.setColumnWidthConstraints(twc3);
        tf3.setWidth( QTextLength( QTextLength::PercentageLength, 100 ) );
        tf3.setBorder(0);
        QTextTable * t3 = cursor.insertTable( 1, 2, tf3 );
        cursor = t3->cellAt( 0, 0 ).firstCursorPosition();
        cursor.insertText( mr.value("auct_cond").toString() , textFormat );
        cursor = t3->cellAt( 0, 1 ).firstCursorPosition();
        QString price = QLatin1String("");
            if ( true != mr.value("auct_pr").isNull() )
            {
            QString price = QString::number( mr.value("auct_pr").toDouble() );
            price.append( QLatin1String(" ") );
            price.append( mr.value("auct_curr").toString() );
            cursor.insertText( price, textFormat );
            }
            else
            {
            cursor.insertText( price, textFormat );
            }
        cursor.setPosition( topFrame->lastPosition() );

        cursor.insertText( mr.value("auct_note").toString(), textFormat );
        cursor.setPosition( topFrame->lastPosition() );
        }
    }

    if ( m.value("catalogs") == QLatin1String("Y") )
    {
    QList< QMap<QString, QVariant> > l;
    l = add_catalogs( table, record.value("ID").toLongLong() );

    QListIterator< QMap<QString, QVariant> > l_i(l);

        if ( l.size() >= 1 )
        {
        QTextBlockFormat f_break;
        f_break.setPageBreakPolicy( QTextFormat::PageBreak_AlwaysBefore );
        cursor.insertBlock(f_break);
        cursor.insertText( QObject::tr("Catalogs") , boldFormat );
        QTextBlockFormat f;
        cursor.insertBlock(f);
        cursor.setPosition( topFrame->lastPosition() );
        }

        while ( true == l_i.hasNext() )
        {
        QTextBlockFormat f;
        cursor.insertBlock(f);
        cursor.insertBlock(f);

        QMap<QString, QVariant> mr = l_i.next();

        QVector<QTextLength> twc1;
        twc1 << QTextLength( QTextLength::PercentageLength, 65 );
        twc1 << QTextLength( QTextLength::PercentageLength, 35 );
        QTextTableFormat tf1;
        tf1.setAlignment( Qt::AlignLeft );
        tf1.setColumnWidthConstraints(twc1);
        tf1.setWidth( QTextLength( QTextLength::PercentageLength, 100 ) );
        tf1.setBorder(0);
        QTextTable * t1 = cursor.insertTable( 1, 2, tf1 );
        cursor = t1->cellAt( 0, 0 ).firstCursorPosition();
        cursor.insertText( mr.value("cat_name").toString() , boldFormat );
        cursor = t1->cellAt( 0, 1 ).firstCursorPosition();
        cursor.insertText( mr.value("int_id").toString() , textFormat );
        cursor.setPosition( topFrame->lastPosition() );
        
        QString note = QObject::tr("Note");
        note.append( QLatin1String(" : ") );
        note.append( mr.value("cat_note").toString() );
        cursor.insertText( note, textFormat );
        cursor.setPosition( topFrame->lastPosition() );

        QVector<QTextLength> twc3;
        twc3 << QTextLength( QTextLength::PercentageLength, 20 );
        twc3 << QTextLength( QTextLength::PercentageLength, 20 );
        twc3 << QTextLength( QTextLength::PercentageLength, 20 );
        twc3 << QTextLength( QTextLength::PercentageLength, 20 );
        twc3 << QTextLength( QTextLength::PercentageLength, 20 );
        QTextTableFormat tf3;
        tf3.setAlignment( Qt::AlignRight );
        tf3.setColumnWidthConstraints(twc3);
        tf3.setWidth( QTextLength( QTextLength::PercentageLength, 80 ) );
        tf3.setBorder(1);
        QTextTable * t3 = cursor.insertTable( 2, 5, tf3 );
        cursor = t3->cellAt( 0, 0 ).firstCursorPosition();
        cursor.insertText( QObject::tr("F"), textFormat );
        cursor = t3->cellAt( 0, 1 ).firstCursorPosition();
        cursor.insertText( QObject::tr("VF"), textFormat );
        cursor = t3->cellAt( 0, 2 ).firstCursorPosition();
        cursor.insertText( QObject::tr("XF"), textFormat );
        cursor = t3->cellAt( 0, 3 ).firstCursorPosition();
        cursor.insertText(  QObject::tr("UNC"), textFormat );
        cursor = t3->cellAt( 0, 4 ).firstCursorPosition();
        cursor.insertText(  QObject::tr("Currency"), textFormat );

        cursor = t3->cellAt( 1, 0 ).firstCursorPosition();
            if ( true != mr.value("cat_pr_f").isNull() )
            {
            cursor.insertText( QString::number( mr.value("cat_pr_f").toDouble() ), textFormat );
            }
            else
            {
            cursor.insertText( QLatin1String(""), textFormat );
            }
        cursor = t3->cellAt( 1, 1 ).firstCursorPosition();
            if ( true != mr.value("cat_pr_vf").isNull() )
            {
            cursor.insertText( QString::number( mr.value("cat_pr_vf").toDouble() ), textFormat );
            }
            else
            {
            cursor.insertText( QLatin1String(""), textFormat );
            }
        cursor = t3->cellAt( 1, 2 ).firstCursorPosition();
            if ( true != mr.value("cat_pr_xf").isNull() )
            {
            cursor.insertText( QString::number( mr.value("cat_pr_xf").toDouble() ), textFormat );
            }
            else
            {
            cursor.insertText( QLatin1String(""), textFormat );
            }
        cursor = t3->cellAt( 1, 3 ).firstCursorPosition();
            if ( true != mr.value("cat_pr_unc").isNull() )
            {
            cursor.insertText( QString::number( mr.value("cat_pr_unc").toDouble() ), textFormat );
            }
            else
            {
            cursor.insertText( QLatin1String(""), textFormat );
            }
        cursor = t3->cellAt( 1, 4 ).firstCursorPosition();
        cursor.insertText( mr.value("cat_curr").toString(), textFormat );
        cursor.setPosition( topFrame->lastPosition() );

        }
    }

    if ( m.value("geo") == QLatin1String("Y") )
    {
    QList< QMap<QString, QVariant> > l;
    l = add_geo( table, record.value("ID").toLongLong() );

    QListIterator< QMap<QString, QVariant> > l_i(l);

        if ( l.size() >= 1 )
        {
        QTextBlockFormat f_break;
        f_break.setPageBreakPolicy( QTextFormat::PageBreak_AlwaysBefore );
        cursor.insertBlock(f_break);
        cursor.insertText( QObject::tr("Geographic") , boldFormat );
        QTextBlockFormat f;
        cursor.insertBlock(f);
        cursor.setPosition( topFrame->lastPosition() );

        QVector<QTextLength> twc0;
        twc0 << QTextLength( QTextLength::PercentageLength, 20 );
        twc0 << QTextLength( QTextLength::PercentageLength, 15 );
        twc0 << QTextLength( QTextLength::PercentageLength, 15 );
        twc0 << QTextLength( QTextLength::PercentageLength, 15 );
        twc0 << QTextLength( QTextLength::PercentageLength, 10 );
        twc0 << QTextLength( QTextLength::PercentageLength, 25 );
        QTextTableFormat tf0;
        tf0.setAlignment( Qt::AlignLeft );
        tf0.setColumnWidthConstraints(twc0);
        tf0.setWidth( QTextLength( QTextLength::PercentageLength, 100 ) );
        tf0.setBorder(0);
        QTextTable * t0 = cursor.insertTable( 1, 6, tf0 );
        cursor = t0->cellAt( 0, 0 ).firstCursorPosition();
        cursor.insertText( QObject::tr("Spot"), boldFormat );
        cursor = t0->cellAt( 0, 1 ).firstCursorPosition();
        cursor.insertText( QObject::tr("Date"), boldFormat );
        cursor = t0->cellAt( 0, 2 ).firstCursorPosition();
        cursor.insertText( QObject::tr("Lat."), boldFormat );
        cursor = t0->cellAt( 0, 3 ).firstCursorPosition();
        cursor.insertText(  QObject::tr("Long."), boldFormat );
        cursor = t0->cellAt( 0, 4 ).firstCursorPosition();
        cursor.insertText(  QObject::tr("Coord. Syst."), boldFormat );
        cursor = t0->cellAt( 0, 5 ).firstCursorPosition();
        cursor.insertText(  QObject::tr("Note"), boldFormat );
        cursor.setPosition( topFrame->lastPosition() );
        }

        while ( true == l_i.hasNext() )
        {
        QTextBlockFormat f;
        cursor.insertBlock(f);
        cursor.insertBlock(f);

        QMap<QString, QVariant> mr = l_i.next();

        QVector<QTextLength> twc1;
        twc1 << QTextLength( QTextLength::PercentageLength, 20 );
        twc1 << QTextLength( QTextLength::PercentageLength, 15 );
        twc1 << QTextLength( QTextLength::PercentageLength, 15 );
        twc1 << QTextLength( QTextLength::PercentageLength, 15 );
        twc1 << QTextLength( QTextLength::PercentageLength, 10 );
        twc1 << QTextLength( QTextLength::PercentageLength, 25 );
        QTextTableFormat tf1;
        tf1.setAlignment( Qt::AlignLeft );
        tf1.setColumnWidthConstraints(twc1);
        tf1.setWidth( QTextLength( QTextLength::PercentageLength, 100 ) );
        tf1.setBorder(0);
        QTextTable * t1 = cursor.insertTable( 1, 6, tf1 );
        cursor = t1->cellAt( 0, 0 ).firstCursorPosition();
        cursor.insertText( mr.value("spot_name").toString(), boldFormat );
        QString date_str = QLatin1String("");
        QDate date = QDate::fromString( QString::number( mr.value("spot_date").toInt() ),
                                        "yyyyMMdd" );
             if ( true == date.isValid() )
             {
             date_str = date.toString("d MMMM yyyy");
             }
        cursor = t1->cellAt( 0, 1 ).firstCursorPosition();
        cursor.insertText( date_str, textFormat );
        cursor = t1->cellAt( 0, 2 ).firstCursorPosition();
            if ( true != mr.value("latitude").isNull() )
            {
            cursor.insertText( QString::number( mr.value("latitude").toDouble() ), textFormat );
            }
            else
            {
            cursor.insertText( QLatin1String(""), textFormat );
            }
        cursor = t1->cellAt( 0, 3 ).firstCursorPosition();
            if ( true != mr.value("longitude").isNull() )
            {
            cursor.insertText( QString::number( mr.value("longitude").toDouble() ), textFormat );
            }
            else
            {
            cursor.insertText( QLatin1String(""), textFormat );
            }
        cursor = t1->cellAt( 0, 4 ).firstCursorPosition();
        cursor.insertText( mr.value("coord").toString(), textFormat );
        cursor = t1->cellAt( 0, 5 ).firstCursorPosition();
        cursor.insertText( mr.value("note").toString(), textFormat );
        cursor.setPosition( topFrame->lastPosition() );
        }
    }

    if ( m.value("other") == QLatin1String("Y") )
    {
    QList< QMap<QString, QVariant> > l;
    l = add_other( table, record.value("ID").toLongLong() );

    QListIterator< QMap<QString, QVariant> > l_i(l);

        if ( l.size() >= 1 )
        {
        QTextBlockFormat f_break;
        f_break.setPageBreakPolicy( QTextFormat::PageBreak_AlwaysBefore );
        cursor.insertBlock(f_break);
        cursor.insertText( QObject::tr("Other References") , boldFormat );
        QTextBlockFormat f;
        cursor.insertBlock(f);
        cursor.setPosition( topFrame->lastPosition() );

        QVector<QTextLength> twc4;
        twc4 << QTextLength( QTextLength::PercentageLength, 20 );
        twc4 << QTextLength( QTextLength::PercentageLength, 30 );
        twc4 << QTextLength( QTextLength::PercentageLength, 35 );
        twc4 << QTextLength( QTextLength::PercentageLength, 15 );

        QTextTableFormat tf4;
        tf4.setAlignment( Qt::AlignLeft );
        tf4.setColumnWidthConstraints(twc4);
        tf4.setWidth( QTextLength( QTextLength::PercentageLength, 100 ) );
        tf4.setBorder(0);
        QTextTable * t4 = cursor.insertTable( 1, 4, tf4 );
        cursor = t4->cellAt( 0, 0 ).firstCursorPosition();
        cursor.insertText( QObject::tr("Name"), boldFormat );
        cursor = t4->cellAt( 0, 1 ).firstCursorPosition();
        cursor.insertText( QObject::tr("Note II"), boldFormat );
        cursor = t4->cellAt( 0, 2 ).firstCursorPosition();
        cursor.insertText( QObject::tr("Note I"), boldFormat );
        cursor = t4->cellAt( 0, 3 ).firstCursorPosition();
        cursor.insertText( QObject::tr("Date"), boldFormat );
        cursor.setPosition( topFrame->lastPosition() );
        }

        while ( true == l_i.hasNext() )
        {
        QTextBlockFormat f;
        cursor.insertBlock(f);
        cursor.insertBlock(f);

        QMap<QString, QVariant> mr = l_i.next();

        QVector<QTextLength> twc5;
        twc5 << QTextLength( QTextLength::PercentageLength, 20 );
        twc5 << QTextLength( QTextLength::PercentageLength, 15 );
        twc5 << QTextLength( QTextLength::PercentageLength, 15 );
        twc5 << QTextLength( QTextLength::PercentageLength, 15 );
        twc5 << QTextLength( QTextLength::PercentageLength, 10 );
        twc5 << QTextLength( QTextLength::PercentageLength, 25 );
        QTextTableFormat tf5;
        tf5.setAlignment( Qt::AlignLeft );
        tf5.setColumnWidthConstraints(twc5);
        tf5.setWidth( QTextLength( QTextLength::PercentageLength, 100 ) );
        tf5.setBorder(0);
        QTextTable * t5 = cursor.insertTable( 1, 4, tf5 );
        cursor = t5->cellAt( 0, 0 ).firstCursorPosition();
        cursor.insertText( mr.value("name").toString(), boldFormat );
        cursor = t5->cellAt( 0, 1 ).firstCursorPosition();
        cursor.insertText( mr.value("text_1").toString(), textFormat );
        cursor = t5->cellAt( 0, 2 ).firstCursorPosition();
        cursor.insertText( mr.value("text_2").toString(), textFormat );
        QString date_str = QLatin1String("");
        QDate date = QDate::fromString( QString::number( mr.value("other_date").toInt() ),
                                        "yyyyMMdd" );
             if ( true == date.isValid() )
             {
             date_str = date.toString("d MMMM yyyy");
             }
        cursor = t5->cellAt( 0, 3 ).firstCursorPosition();
        cursor.insertText( date_str, textFormat );

        cursor.setPosition( topFrame->lastPosition() );
        }
    }
}


QTextDocument * CreateDoc::return_doc()
{
return &doc;
}

QString CreateDoc::add_image( QString table,
                              qint64 id,
                              QString side )
{
QString icon = QLatin1String("");

QSqlDatabase db = QSqlDatabase::database("collection", false);
    if ( true != db.isOpen() )
    {
    return icon;
    }

QImage image;
QString image_name;
QByteArray ba;
qint64 image_id = Q_INT64_C(-1);
QString saved_as;

QString s = QLatin1String("select image_id, file_name,saved_as from ");
s.append(table);
s.append( QLatin1String("_images where coin_id = ") );
s.append( QString::number(id) );
    if ( side == QLatin1String("obverse") )
    {
    s.append( QLatin1String(" and is_obv_icon = 1") );
    }
    else if ( side == QLatin1String("reverse") )
    {
    s.append( QLatin1String(" and is_rev_icon = 1") );
    }
    else if ( side == QLatin1String("edge") )
    {
    s.append( QLatin1String(" and is_edge_icon = 1") );
    }
    else
    {
    return QString( QLatin1String("") );
    }

QSqlQuery query(s, db);
    while (query.next())
    {
    image_id = query.value(0).toLongLong();
    image_name = query.value(1).toString();
    saved_as = query.value(2).toString();
    }
query.clear();

QString ext = image_name.section(".", -1);

    if ( saved_as == QString( QLatin1String("BLOB") ) )
    {
    icon = table;
    icon.append( QLatin1String("_") );
    icon.append( QString::number(image_id) );
    icon.append( QLatin1String(".") );
    icon.append(ext);

    QString s1 = QLatin1String("select image_b from ");
    s1.append(table);
    s1.append( QLatin1String("_im_blobs where image_id = ") );
    s1.append( QString::number(image_id) );
    QSqlQuery query1(s1, db);
        while (query1.next())
        {
        ba = query1.value(0).toByteArray();
        }
    query1.clear();
    
    const QByteArray & ba_ref = ba;
    
        if ( ext == QLatin1String("SVG") ||
             ext == QLatin1String("svg") )
        {
        image.loadFromData( ba_ref, "svg" );
        }
        else
        {
        image.loadFromData(ba_ref);
        }
    }
    else if ( saved_as == QString( QLatin1String("LINK") ) )
    {
    icon = table;
    icon.append( QLatin1String("_") );
    icon.append( QString::number(image_id) );
    icon.append( QLatin1String(".") );
    icon.append(ext);
        if ( ext == QLatin1String("SVG") ||
             ext == QLatin1String("svg") )
        {
        image.load( image_name, "svg" );
        }
        else
        {
        image.load(image_name);
        }
    }
    else
    {
    }

qApp->processEvents();

    if ( true != image.isNull() )
    {
        if ( side == QLatin1String("edge") )
        {
        image = image.scaledToHeight(75);
        int width = image.width();
            if ( width > 510 )
            {
            image = image.scaledToWidth(510);
            }
        }
        else
        {
        image = image.scaledToHeight(250);
        int width = image.width();
            if ( width > 250 )
            {
            image = image.scaledToWidth(250);
            }
        }

    qApp->processEvents();

    doc.addResource(QTextDocument::ImageResource, icon, image);
    }
    else
    {
    return QString( QLatin1String("") );
    }

return icon;
}

QString CreateDoc::add_desc( QString table,
                             qint64 id )
{
QSqlDatabase db = QSqlDatabase::database("collection", false);
    if ( true != db.isOpen() )
    {
    return QString( QLatin1String("") );
    }

QByteArray ba;

QString s = QLatin1String("select desc_b from ");
s.append(table);
s.append( QLatin1String("_desc_blobs where coin_id = ") );
s.append( QString::number(id) );

QSqlQuery query(s, db);
    while ( query.next() )
    {
    ba = query.value(0).toByteArray();
    }
query.clear();
QString t = QString::fromUtf8( ba.constData() );
return t;
}

QList< QMap<QString, QVariant> > CreateDoc::add_auctions( QString table,
                                                          qint64 id )
{
QList< QMap<QString, QVariant> > l;
QSqlDatabase db = QSqlDatabase::database("collection", false);
    if ( true != db.isOpen() )
    {
    return l;
    }

QString s = QLatin1String("select auct_name, auct_date");
s.append( QLatin1String(", auct_note, auct_cond, auct_pr, auct_curr from ") );
s.append(table);
s.append( QLatin1String("_auctions where coin_id = ") );
s.append( QString::number(id) );
s.append( QLatin1String(" order by auct_date") );

QSqlQuery query(s, db);
    while ( query.next() )
    {
    QMap<QString, QVariant> mr;
    mr[ QLatin1String("auct_name") ] = query.value(0);
    mr[ QLatin1String("auct_date") ] = query.value(1);
    mr[ QLatin1String("auct_note") ] = query.value(2);
    mr[ QLatin1String("auct_cond") ] = query.value(3);
    mr[ QLatin1String("auct_pr") ] = query.value(4);
    mr[ QLatin1String("auct_curr") ] = query.value(5);
    l << mr;
    }
query.clear();
return l;
}

QList< QMap<QString, QVariant> > CreateDoc::add_catalogs( QString table,
                                                          qint64 id )
{
QList< QMap<QString, QVariant> > l;
QSqlDatabase db = QSqlDatabase::database("collection", false);
    if ( true != db.isOpen() )
    {
    return l;
    }

QString s = QLatin1String("select cat_name,int_id");
s.append( QLatin1String(",cat_note,cat_pr_f,cat_pr_vf,cat_pr_xf,cat_pr_unc,cat_curr from ") );
s.append(table);
s.append( QLatin1String("_catalogs where coin_id = ") );
s.append( QString::number(id) );

QSqlQuery query(s, db);
    while ( query.next() )
    {
    QMap<QString, QVariant> mr;
    mr[ QLatin1String("cat_name") ] = query.value(0);
    mr[ QLatin1String("int_id") ] = query.value(1);
    mr[ QLatin1String("cat_note") ] = query.value(2);
    mr[ QLatin1String("cat_pr_f") ] = query.value(3);
    mr[ QLatin1String("cat_pr_vf") ] = query.value(4);
    mr[ QLatin1String("cat_pr_xf") ] = query.value(5);
    mr[ QLatin1String("cat_pr_unc") ] = query.value(6);
    mr[ QLatin1String("cat_curr") ] = query.value(7);
    l << mr;
    }
query.clear();
return l;
}

QList< QMap<QString, QVariant> > CreateDoc::add_geo( QString table,
                                                     qint64 id )
{
QList< QMap<QString, QVariant> > l;
QSqlDatabase db = QSqlDatabase::database("collection", false);
    if ( true != db.isOpen() )
    {
    return l;
    }

QString s = QLatin1String("select spot_name,note");
s.append( QLatin1String(",coord,latitude,longitude,spot_date from ") );
s.append(table);
s.append( QLatin1String("_spots where coin_id = ") );
s.append( QString::number(id) );
s.append( QLatin1String(" order by spot_date") );

QSqlQuery query(s, db);
    while ( query.next() )
    {
    QMap<QString, QVariant> mr;
    mr[ QLatin1String("spot_name") ] = query.value(0);
    mr[ QLatin1String("note") ] = query.value(1);
    mr[ QLatin1String("coord") ] = query.value(2);
    mr[ QLatin1String("latitude") ] = query.value(3);
    mr[ QLatin1String("longitude") ] = query.value(4);
    mr[ QLatin1String("spot_date") ] = query.value(5);
    l << mr;
    }
query.clear();
return l;
}

QList< QMap<QString, QVariant> > CreateDoc::add_other( QString table,
                                                       qint64 id )
{
QList< QMap<QString, QVariant> > l;
QSqlDatabase db = QSqlDatabase::database("collection", false);
    if ( true != db.isOpen() )
    {
    return l;
    }

QString s = QLatin1String("select name,text_1,text_2,other_date from ");
s.append(table);
s.append( QLatin1String("_other where coin_id = ") );
s.append( QString::number(id) );
s.append( QLatin1String(" order by other_date") );

QSqlQuery query(s, db);
    while ( query.next() )
    {
    QMap<QString, QVariant> mr;
    mr[ QLatin1String("name") ] = query.value(0);
    mr[ QLatin1String("text_1") ] = query.value(1);
    mr[ QLatin1String("text_2") ] = query.value(2);
    mr[ QLatin1String("other_date") ] = query.value(3);
    l << mr;
    }
query.clear();
return l;
}
